package com.example.twopointers;

import java.util.Arrays;

public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] arr=new int[]{1,2,2,3,3,4};
        System.out.println(duplicates(arr));
        Arrays.stream(arr).forEach(System.out::print);
    }
    public static int duplicate(int[] arr){
        if (arr.length<=1){
            return arr.length;
        }
        int i=1;
        int j=1;
        while (j<arr.length){
            if (arr[j]!=arr[i-1]){
                arr[i]=arr[j];
                i++;j++;
            }else {
                j++;
            }
        }
        return i;
    }

    public static int duplicates(int[] arr){
        int i=0;
        int j=0;
        while (j<arr.length){
            if (i==0 || arr[i-1]!=arr[j]){
                arr[i++]=arr[j++];
            }else {
                j++;
            }
        }
        return i;
    }
}
